package com.yiren.dbaa.module.practice_set.line.model;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.line.contract.BaiduMapContract;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/11/21
 */
public class BaiduMapModel implements BaiduMapContract.Model {

    /*** 是否首次定位 */
    private boolean isFirstLoc = true;

    @Override
    public BaiduMap getBaiduMap(TextureMapView mMapView) {
        LocationMode mCurrentMode = LocationMode.FOLLOWING;
        // 隐藏百度地图logo
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        // 地图上比例尺
        mMapView.showScaleControl(true);
        // 地图缩放控件
        mMapView.showZoomControls(true);
        BaiduMap mBaiduMap = mMapView.getMap();
        // 设置地图类型:默认图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 设置定位图标
//        mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.car_modelsmall);
        BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.car);
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
        mBaiduMap.setMyLocationConfiguration(config);
        // 设置缩放比率
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(20);
        mBaiduMap.setMapStatus(msu);
        mBaiduMap.showMapPoi(true);
        UiSettings mUiSettings = mBaiduMap.getUiSettings();
        // 设置是否允许所有手势操作
        mUiSettings.setAllGesturesEnabled(true);
        // 设置是否允许指南针
        mUiSettings.setCompassEnabled(true);
        // 设置是否允许俯视手势
        mUiSettings.setOverlookingGesturesEnabled(true);
        // 设置是否允许旋转手势
        mUiSettings.setRotateGesturesEnabled(true);
        // 设置是否允许拖拽手势
        mUiSettings.setScrollGesturesEnabled(true);
        // 设置是否允许缩放手势
        mUiSettings.setZoomGesturesEnabled(true);
        return mBaiduMap;
    }

    @Override
    public BaiduMap getBaiduMap(MapView mMapView) {
        LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
        // 隐藏百度地图logo
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        // 地图上比例尺
        mMapView.showScaleControl(true);
        // 地图缩放控件
        mMapView.showZoomControls(true);
        BaiduMap mBaiduMap = mMapView.getMap();
        // 设置地图类型:默认图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 设置定位图标
        BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.car);
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
        mBaiduMap.setMyLocationConfiguration(config);
        // 设置缩放比率
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(19);
        mBaiduMap.setMapStatus(msu);
        mBaiduMap.showMapPoi(true);
        UiSettings mUiSettings = mBaiduMap.getUiSettings();
        // 设置是否允许所有手势操作
        mUiSettings.setAllGesturesEnabled(true);
        // 设置是否允许指南针
        mUiSettings.setCompassEnabled(true);
        // 设置是否允许俯视手势
        mUiSettings.setOverlookingGesturesEnabled(true);
        // 设置是否允许旋转手势
        mUiSettings.setRotateGesturesEnabled(true);
        // 设置是否允许拖拽手势
        mUiSettings.setScrollGesturesEnabled(true);
        // 设置是否允许缩放手势
        mUiSettings.setZoomGesturesEnabled(true);
        return mBaiduMap;
    }

    @Override
    public void updateLocationInfo(BaiduMap mBaiduMap, float bearing, double latitude, double longitude) {
        if (null != mBaiduMap) {
            // map view 销毁后不在处理新接收的位置
            MyLocationData locData = new MyLocationData.Builder().accuracy(20)// 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(1).latitude(latitude).longitude(longitude).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(latitude, longitude);
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                if (null != mBaiduMap) {
                    mBaiduMap.animateMapStatus(u);
                }
            }
        }
    }

    @Override
    public void drawBaiduMarker(Activity mActivity, List<LinePointBean> pointList, BaiduMap mBaiduMap, int currentPointIndex) {
        List<LatLng> points = new ArrayList<LatLng>();
        for (int i = 0; i < pointList.size(); i++) {
            LinePointBean linePoint = pointList.get(i);
            if (mBaiduMap == null) break;
            // 添加折线
            LatLng p1 = new LatLng(linePoint.getLatitude(), linePoint.getLongitude());
            points.add(p1);
            if (points.size() >= 1) {
                LatLng lastPoint = points.get(points.size() - 1);
                View view = View.inflate(mActivity.getApplicationContext(), R.layout.item_line_point_tag, null);
                TextView tvPointName = (TextView) view.findViewById(R.id.tv_point_name);
                ImageView ivPointTag = (ImageView) view.findViewById(R.id.iv_point_tag);
                if (currentPointIndex == i) {
                    tvPointName.setTextColor(mActivity.getResources().getColor(R.color.red));
                    ivPointTag.setImageResource(R.mipmap.map_point_red);
                } else {
                    ivPointTag.setImageResource(R.mipmap.map_point_blue);
                }
                if (null != linePoint.getProjectInfo() && null != linePoint.getProjectInfo().getName()) {
                    tvPointName.setText((i + 1) + "." + linePoint.getProjectInfo().getName());
                }
                BitmapDescriptor descriptor = BitmapDescriptorFactory.fromView(view);
                OverlayOptions optionImage = new MarkerOptions().position(lastPoint).rotate(0).icon(descriptor).zIndex(i).draggable(false);
                mBaiduMap.addOverlay(optionImage);
            }
        }
    }
}
